package com.ai.view;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ai.mmd.reviewed.vo.SbmxinfoVO;

/**
 * 导出特色航线Excel视图
 * 
 * @author Andy
 *
 */
public class shreportViewExcel extends AbstractExcelView {

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String excelName = "审核记录表.xls";
		// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(excelName, "UTF-8"));

		// 产生Excel表头
		HSSFSheet sheet = workbook.createSheet("审核记录表");
		
		//标题样式 start
		HSSFCellStyle headStyle = workbook.createCellStyle();
		headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  

		HSSFFont titlefont = workbook.createFont();
		titlefont.setFontHeightInPoints((short) 22);
		headStyle.setFont(titlefont);
		//标题样式 end

		HSSFCellStyle titleStyle = workbook.createCellStyle();
		titleStyle.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
		titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  

		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
		titleStyle.setFont(font);
		
		//单元格样式
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  
		
		HSSFRow title = sheet.createRow(0);
		title.setHeight((short) 500);
		HSSFCell hsstitle = title.createCell(0);
		hsstitle.setCellValue("审核记录表");
		hsstitle.setCellStyle(headStyle);
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10));

		HSSFRow headwh = sheet.createRow(1);
		headwh.setHeight((short) 400);
		
		sheet.setColumnWidth(1, 5000);
		sheet.setColumnWidth(4, 5000);
		sheet.setColumnWidth(6, 5000);
		sheet.setColumnWidth(7, 5000);
		sheet.setColumnWidth(10, 5000);
		
		HSSFCell h_xhao = headwh.createCell(0);
		h_xhao.setCellValue("序号");
		h_xhao.setCellStyle(titleStyle);
		
		HSSFCell h_qymc = headwh.createCell(1);
		h_qymc.setCellValue("企业名称");
		h_qymc.setCellStyle(titleStyle);

		HSSFCell h_sbnf = headwh.createCell(2);
		h_sbnf.setCellValue("申报年份");
		h_sbnf.setCellStyle(titleStyle);
		
		HSSFCell h_sbjd = headwh.createCell(3);
		h_sbjd.setCellValue("申报季度");
		h_sbjd.setCellStyle(titleStyle);
		
		HSSFCell h_xh = headwh.createCell(4);
		h_xh.setCellValue("箱号");
		h_xh.setCellStyle(titleStyle);
		
		HSSFCell h_xx = headwh.createCell(5);
		h_xx.setCellValue("箱型");
		h_xx.setCellStyle(titleStyle);
		
		HSSFCell h_tlhph = headwh.createCell(6);
		h_tlhph.setCellValue("铁路货票号");
		h_tlhph.setCellStyle(titleStyle);
		
		HSSFCell h_hyydh = headwh.createCell(7);
		h_hyydh.setCellValue("货物运单号");
		h_hyydh.setCellStyle(titleStyle);
		
		HSSFCell h_rzjlb = headwh.createCell(8);
		h_rzjlb.setCellValue("入闸记录表");
		h_rzjlb.setCellStyle(titleStyle);
		
		HSSFCell h_rzhanjlb = headwh.createCell(9);
		h_rzhanjlb.setCellValue("入站记录表");
		h_rzhanjlb.setCellStyle(titleStyle);
		
		HSSFCell h_jjyy = headwh.createCell(10);
		h_jjyy.setCellValue("审核描述");
		h_jjyy.setCellStyle(titleStyle);

		List<SbmxinfoVO> list = (List<SbmxinfoVO>) model.get("list");

		for (int i=2;i<=list.size()+1;i++) {
			HSSFRow row = sheet.createRow(i);
			
			//序号
			HSSFCell r_xhao = row.createCell(0);
			HSSFRichTextString richxhao = new HSSFRichTextString(String.valueOf(i-1));
			r_xhao.setCellValue(richxhao);
			r_xhao.setCellStyle(cellStyle);
			
			//企业名称
			HSSFCell r_cmhc = row.createCell(1);
			HSSFRichTextString richcmhc = new HSSFRichTextString(list.get(i-2).getQyqc());
			r_cmhc.setCellValue(richcmhc);
			r_cmhc.setCellStyle(cellStyle);
			
			//申报年份
			HSSFCell r_sbnf = row.createCell(2);
			r_sbnf.setCellStyle(cellStyle);
			if (list.get(i-2).getSbnf()!=null) {
				HSSFRichTextString richsbnf = new HSSFRichTextString(list.get(i-2).getSbnf().toString());
				r_sbnf.setCellValue(richsbnf);	
			}
			
			//申报季度
			HSSFCell r_sbjd = row.createCell(3);
			r_sbjd.setCellStyle(cellStyle);
			if (list.get(i-2).getSbjd()!=null) {
				HSSFRichTextString richsbjd = new HSSFRichTextString(list.get(i-2).getSbjd().toString());
				r_sbjd.setCellValue(richsbjd);
			}
			
			//箱号
			HSSFCell r_xh = row.createCell(4);
			r_xh.setCellStyle(cellStyle);
			if (list.get(i-2).getXh()!=null) {
				HSSFRichTextString richxh = new HSSFRichTextString(list.get(i-2).getXh());
				r_xh.setCellValue(richxh);
			}

			//箱型
			HSSFCell r_xx = row.createCell(5);
			r_xx.setCellStyle(cellStyle);
			if (list.get(i-2).getXxinfoVO()!=null) {
				HSSFRichTextString richxx = new HSSFRichTextString(list.get(i-2).getXxinfoVO().getXxmc());
				r_xx.setCellValue(richxx);
			}
			
			//铁路货票号
			HSSFCell r_tlhph = row.createCell(6);
			r_tlhph.setCellStyle(cellStyle);
			if (list.get(i-2).getTlhph()!=null) {
				HSSFRichTextString richtlhph = new HSSFRichTextString(list.get(i-2).getTlhph());
				r_tlhph.setCellValue(richtlhph);
			}
			
			//货物运单号
			HSSFCell r_hwydh = row.createCell(7);
			r_hwydh.setCellStyle(cellStyle);
			if (list.get(i-2).getHwydh()!=null) {
				HSSFRichTextString richhwydh = new HSSFRichTextString(list.get(i-2).getHwydh());
				r_hwydh.setCellValue(richhwydh);
			}
			
			//入闸记录表
			HSSFCell r_rzjlb = row.createCell(8);
			r_rzjlb.setCellStyle(cellStyle);
			HSSFRichTextString richrzjlb = new HSSFRichTextString("√");
			r_rzjlb.setCellValue(richrzjlb);
			
			//入站记录表
			HSSFCell r_rzhanjlb = row.createCell(9);
			r_rzhanjlb.setCellStyle(cellStyle);
			HSSFRichTextString richrzhanjlb = new HSSFRichTextString("√");
			r_rzhanjlb.setCellValue(richrzhanjlb);

			//审核描述
			HSSFCell r_jjyy = row.createCell(10);
			r_jjyy.setCellStyle(cellStyle);
			if (list.get(i-2).getJjyy()!=null&&!"".equals(list.get(i-2).getJjyy())) {
				HSSFRichTextString richjjyy = new HSSFRichTextString(list.get(i-2).getJjyy());
				r_jjyy.setCellValue(richjjyy);
			}else{
				HSSFRichTextString richjjyy = new HSSFRichTextString("通过");
				r_jjyy.setCellValue(richjjyy);
			}
		}
		HSSFRow row = sheet.createRow(list.size()+2);
		HSSFCell r_czrcell = row.createCell(9);
		r_czrcell.setCellValue("操作人:");
		HSSFCell r_czrvalue = row.createCell(10);
		r_czrvalue.setCellValue(model.get("name").toString());
	}
}
